Skip to content

fix(ios): restrict native bridge handlers to the main frame - #8740

Open
basnijholt wants to merge 3 commits into
ionic-team:mainfrom
basnijholt:fix/ios-bridge-main-frame-only
Open

basnijholt wants to merge 3 commits into
ionic-team:mainfrom
basnijholt:fix/ios-bridge-main-frame-only

Conversation

@basnijholt

Copy link
Copy Markdown

Description

Enforce main-frame-only execution in WebViewDelegationHandler using WebKit's native frame metadata:

  • In userContentController(_:didReceive:), return early (logging "⚡️ Plugin execution is allowed in Main Frame only") when !message.frameInfo.isMainFrame.
  • In webView(_:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler:), only intercept synchronous CapacitorCookies.get / CapacitorCookies.set prompts when frame.isMainFrame is true, letting regular subframe window.prompt() calls fall through normally.
  • In webView(_:runJavaScriptTextInputPanelWithPrompt:...), call completionHandler(nil) before returning when bridge?.viewController is nil, so a subframe cookie prompt (or any prompt without a presenter) never drops WebKit's completion handler.

Added regression tests in ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift (sharing StubFrameInfo with HttpInterceptorNavigationTests.swift).

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

Although JSExport injects all iOS bridge scripts with forMainFrameOnly: true and Android's MessageHandler (WebMessageListener path) enforces if (isMainFrame) before dispatching messages, WebKit exposes window.webkit.messageHandlers.bridge and runJavaScriptTextInputPanelWithPrompt to every frame in the WKWebView (including cross-origin and sandboxed iframes). Without an isMainFrame check in WebViewDelegationHandler, subframes can invoke native plugins directly via webkit.messageHandlers.bridge.postMessage(...) or read/write native cookies via window.prompt(...).

No related issue: security hardening, see description.

Tests or Reproductions

Added ios/Tests/CapacitorTests/WebViewDelegationHandlerTests.swift covering:

  • Subframe WKScriptMessage is ignored by userContentController(_:didReceive:).
  • Main-frame WKScriptMessage is dispatched normally.
  • Subframe window.prompt with a CapacitorCookies.get payload does not invoke CapacitorCookieManager and falls through to normal prompt presentation.
  • Subframe window.prompt with a CapacitorCookies.get payload invokes completionHandler(nil) exactly once when bridge is alive but viewController is nil.
  • Main-frame window.prompt with CapacitorCookies.get still resolves synchronously without presenting an alert.

Built and tested on macOS (Xcode 26.6, iPhone 17 / iOS 26.5 simulator):

  • xcodebuild build -scheme Capacitor-Package -destination 'generic/platform=iOS Simulator' — BUILD SUCCEEDED
  • xcodebuild test -scheme Capacitor-Package -destination 'platform=iOS Simulator,name=iPhone 17,OS=26.5' -collect-test-diagnostics never — TEST SUCCEEDED:
    • CapacitorObjCTests.xctest: 6/6 passed
    • CapacitorTests.xctest (XCTest): 22/22 passed (including 5/5 in WebViewDelegationHandlerTests and 3/3 in HttpInterceptorNavigationTests)
    • Swift Testing: 132/132 passed in 19 suites

Not run locally:

  • npm run swiftlint -- lint (unverified; CI will check).

Screenshots / Media

N/A

Platforms Affected

  • Android
  • iOS
  • Web

Notes / Comments

  • Iframe impact: Same-origin iframes calling plugins through parent.Capacitor should be unaffected (calls are sent through the parent window's handler and prompt); only code posting directly to webkit.messageHandlers.bridge (or forged CapacitorCookies.* prompts) from a subframe stops working, matching Android's MessageHandler WebMessageListener path (Android's legacy addJavascriptInterface fallback and CapacitorCookiesAndroidInterface do not check frames).
  • Target branch: Targeted at main as a security bug fix; maintainers may prefer next if they treat blocking direct subframe bridge posts as a breaking change (and 8.x has the same code under ios/Capacitor/Capacitor/WebViewDelegationHandler.swift if a backport is wanted).
  • AI assistance: Written with MindRoom DevAgent (google/gemini-4-neon); the xcodebuild build and xcodebuild test commands listed under Tests were run on macOS (Xcode 26.6, iPhone 17 / iOS 26.5 simulator); SwiftLint was not run locally.

@andredestro andredestro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I ran it locally against current main (Xcode 27.2, iPhone 17 / iOS 27.0 simulator) and have a few questions:

  1. Once rebased on main, the test target doesn't compile: FileProxyRouteTests.swift (added after this PR was opened) declares its own private class StubFrameInfo, which now clashes with the shared one (invalid redeclaration of 'StubFrameInfo'). Could we drop the private copy there and use the shared stub? With that change the full suite passes for me (XCTest 32/32, Swift Testing 132/132).
  2. runJavaScriptConfirmPanelWithMessage also returns without calling its completion handler when there's no view controller, like the prompt case you fixed. Should we call completionHandler(false) there too?
  3. Nit: now that StubFrameInfo has both variants, would .subframe read better than .shared?

I also checked in a real WKWebView that calls made through parent (like parent.Capacitor) are still attributed to the main frame, while direct posts from an iframe, sandboxed or not, come in as subframe. So the iframe impact described in the PR holds.

@basnijholt

Copy link
Copy Markdown
Author

Thanks for testing it on a real WKWebView! I merged main and pushed 5680cca, which does all three:

  1. FileProxyRouteTests now uses the shared StubFrameInfo instead of its private copy.
  2. runJavaScriptConfirmPanelWithMessage calls completionHandler(false) when there's no view controller, and there's a test for it.
  3. Renamed .shared to .subframe.

On Xcode 27.2 with the iPhone 17 simulator, xcodebuild test -scheme Capacitor-Package passes: XCTest 33/33 and Swift Testing 132/132.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants